package com.twt.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.twt.common.exceptions.AppException;
import com.twt.common.utils.Assert;
import com.twt.common.utils.MD5Util;
import com.twt.model.system.entity.SysAccount;
import com.twt.model.system.vo.SysAccountAddVo;
import com.twt.sys.mapper.SysAccountMapper;
import com.twt.sys.service.SysAccountService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * @Desciption:
 * @ClassName:SysAccountImpl
 * @Author:TwT
 * @Date:2022/6/15 23:06
 * @Version:1.0
 **/
@Service
@DS("sys")
public class SysAccountImpl extends ServiceImpl<SysAccountMapper, SysAccount> implements SysAccountService {

    private final String DEFAULT_PASSWORD = "123456";

    /**
     * @Description: 由管理员添加账号
     * @Author: TWT
     * @Date: 21:03 2022/6/22
     * @param:[sysAccount]
     * @return:void
     **/
    @Override
    public void addAccount(SysAccount sysAccount) {
        this.checkAccountExistThr(sysAccount.getAccount());
        sysAccount.setId(IdUtil.getSnowflakeNextIdStr());
        sysAccount.setAccType("01");
        sysAccount.setPassword(MD5Util.encode(DEFAULT_PASSWORD));
        this.save(sysAccount);
    }

    /**
     * @Description: 注册账号
     * @Author: TWT
     * @Date: 21:03 2022/6/22
     * @param:[sysAccountAddVo]
     * @return:void
     **/
    @Override
    public void registryAccount(SysAccountAddVo sysAccountAddVo) {
        String password = sysAccountAddVo.getPassword();
        String confirmPassword = sysAccountAddVo.getConfirmPassword();
        if (!password.equals(confirmPassword)) {
            throw new AppException("两次输入密码不一致，请重新输入");
        }
        SysAccount sysAccount = new SysAccount();
        BeanUtil.copyProperties(sysAccountAddVo, sysAccount);

        sysAccount.setId(IdUtil.getSnowflakeNextIdStr());
        sysAccount.setPassword(MD5Util.encode(sysAccount.getPassword()));

        this.save(sysAccount);
    }

    /**
     * @Description: 校验正好是否存在
     * @Author: TWT
     * @Date: 21:04 2022/6/22
     * @param:[account]
     * @return:java.lang.Boolean
     **/
    @Override
    public Boolean checkAccountExist(String account) {
        Assert.isBlank(account, "校验账号不能为空!");
        LambdaQueryWrapper<SysAccount> queryWrapper = Wrappers.lambdaQuery(SysAccount.class).eq(SysAccount::getAccount, account);
        int count = this.count(queryWrapper);

        return count > 0;
    }

    private void checkAccountExistThr(String account) {
        Boolean aBoolean = this.checkAccountExist(account);
        if (aBoolean) throw new AppException("登录账号已存在!");
    }
}
